/***************************************************************************
 * 文件名：XmlUtil
 * 功能：
 * 说明： XML工具类
 * -------------------------------------------------------------------------
 * 创建时间：2025/7/10 9:07:10
 * 创建人：葛浩
 * 邮箱： 2094733071@qq.com
 * =========================================================================
 *
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

namespace Util
{
	public class XmlUtil
	{
		#region 数据库配置操作

		/// <summary>
		/// 创建默认配置文件
		/// </summary>
		/// <param name="filePath"></param>
		public static void GeneratorAppConfig(string filePath)
		{
			// 创建 XML 文档结构
			XmlDocument doc = new XmlDocument();
			XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", "");
			doc.AppendChild(xmlDeclaration);

			// 创建根节点 <configuration>
			XmlElement root = doc.CreateElement("configuration");
			doc.AppendChild(root);

			// 创建 <connectionStrings> 节点
			XmlElement connectionStrings = doc.CreateElement("connectionStrings");
			root.AppendChild(connectionStrings);

			// 保存到文件
			doc.Save(filePath);
		}

		/// <summary>
		/// 添加数据库连接
		/// </summary>
		/// <param name="filePath"></param>
		/// <param name="name"></param>
		/// <param name="connectionString"></param>
		/// <returns></returns>
		public static bool AddConnectionStrings(string filePath, string name, string connectionString)
		{
			if (string.IsNullOrEmpty(filePath) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(connectionString))
			{
				return false;
			}

			XmlDocument document = new XmlDocument();
			document.Load(filePath);
			if (document == null)
			{
				return false;
			}

			XmlNode singleNode = document.SelectSingleNode("//connectionStrings");

			if (singleNode == null)
			{
				return false;
			}

			//添加主机连接字符串
			XmlElement setting = document.CreateElement("add");
			setting.SetAttribute("name", name);
			setting.SetAttribute("connectionString", connectionString);
			setting.SetAttribute("providerName", "System.Data.SqlClient");
			singleNode.AppendChild(setting);

			//保存文件
			document.Save(filePath);

			return true;
		}

		/// <summary>
		/// 删除数据库连接
		/// </summary>
		/// <param name="filePath"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public static bool DeleteConnectString(string filePath, string name)
		{
			if (string.IsNullOrEmpty(filePath) || string.IsNullOrEmpty(name))
			{
				return false;
			}

			XmlDocument document = new XmlDocument();
			document.Load(filePath);
			if (document == null)
			{
				return false;
			}

			XmlNode singleNode = document.SelectSingleNode("//connectionStrings");

			if (singleNode?.ChildNodes.Count == 0)
			{
				return false;
			}

			foreach (XmlNode node in singleNode.ChildNodes)
			{
				if (node == null) { continue; }
				string connectName = node.Attributes["name"].Value;
				if (string.IsNullOrEmpty(connectName) || !connectName.Equals(name)) { continue; }

				//删除主机连接字符串
				singleNode.RemoveChild(node);
				break;
			}

			//保存文件
			document.Save(filePath);

			return true;
		}

		/// <summary>
		/// 添加appSetting节点
		/// </summary>
		/// <param name="filePath"></param>
		/// <param name="key"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public static bool AddSetting(string filePath, string key, string value)
		{
			if (string.IsNullOrEmpty(filePath) || string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
			{
				return false;
			}

			XmlDocument document = new XmlDocument();
			document.Load(filePath);
			if (document == null)
			{
				return false;
			}

			XmlElement appSettingtElement = document.GetElementById("appSettings");

			if (appSettingtElement == null)
			{
				return false;
			}


			XmlElement setting = document.CreateElement("add");
			setting.SetAttribute("key", key);
			setting.SetAttribute("value", value);
			appSettingtElement.AppendChild(setting);

			//保存文件
			document.Save(filePath);

			return true;
		}

		#endregion
	}
}
